package com.gitee.feizns.explore.data_structure.tree.bs.easy;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author feizns
 * @since 2019/12/24
 */
public class LevelOrderBottom {

    public static void main(String[] args) {
        TreeNode node = create();
        System.out.println(levelOrderBottom(node));
    }

    public static List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ret = new LinkedList<>();
        if ( root != null ) {
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            while ( !queue.isEmpty() ) {
                List<Integer> tmp = new LinkedList<>();
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.remove();
                    tmp.add(node.val);
                    if ( node.left != null )
                        queue.add(node.left);
                    if ( node.right != null )
                        queue.add(node.right);
                }
                ret.add(tmp);
            }
            Collections.reverse(ret);
        }
        return ret;
    }

    private static TreeNode create() {
        TreeNode tmp = new TreeNode(3);
        tmp.left = new TreeNode(9);
        tmp.right = new TreeNode(10);
        tmp.right.left = new TreeNode(11);
        tmp.right.right = new TreeNode(12);
        return tmp;
    }

//    public static List<TreeNode> levelOrderBottom(TreeNode next) {
//
//    }

}
